package com.example.redistest.config;

import com.example.redistest.pojo.Student;
import com.example.redistest.pojo.TT;
import com.example.redistest.pojo.TokenInfo;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.*;
import java.util.stream.Collectors;

/**
 * @author zjq
 * @since 2022/6/10 18:47
 */
public class FutureTest {
    private static ExecutorService cachePool = Executors.newCachedThreadPool();
    private static ExecutorService executors = Executors.newCachedThreadPool();


    public static void main(String[] args) throws ExecutionException, InterruptedException {

        Integer[] idArr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
        List<Integer> idList = Arrays.asList(idArr);
        List<Integer> l1 = idList.stream().filter(e -> e % 4 == 0).collect(Collectors.toList());
        List<Integer> l2 = idList.stream().filter(e -> e % 4 == 1).collect(Collectors.toList());
        List<Integer> l3 = idList.stream().filter(e -> e % 4 == 2).collect(Collectors.toList());
        List<Integer> l4 = idList.stream().filter(e -> e % 4 == 3).collect(Collectors.toList());
        System.out.println(1);

        //  第二个任务 是 主线程
//        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
//            System.out.println("123线程：" + Thread.currentThread().getName());
//            return 123;
//        }, executors);
//        CompletableFuture<Integer> fun = future.thenApplyAsync(integer -> {
//            System.out.println(Thread.currentThread().getName() + "上个任务执行结果:" + integer);
//            return 3333;
//        }, executors);
//        System.out.println(Thread.currentThread().getName() + "第二个任务:" + fun.get());


        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
            System.out.println("123线程：" + Thread.currentThread().getName());
            return 123;
        }, executors);
        CompletableFuture<Integer> fun = future.thenApply(integer -> {
            System.out.println(Thread.currentThread().getName() + "上个任务执行结果:" + integer);
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return 3333;
        });
        System.out.println(Thread.currentThread().getName() + "第二个任务:" + fun.get());


        FutureTest test = new FutureTest();

//        long l = System.currentTimeMillis();
////        test.futureTest();
//        test.completeFutureTest();
//
//        System.out.println("耗时：" + (System.currentTimeMillis() - l));


    }

    private void completeFutureTest() {
        long l = System.currentTimeMillis();
        CompletableFuture<List<Student>> f1 = CompletableFuture.supplyAsync(this::getDms);
        CompletableFuture<List<TokenInfo>> f2 = CompletableFuture.supplyAsync(this::getDmsFs);

        //getNow有点特殊，如果结果已经计算完则返回结果或者抛出异常，否则返回给定的valueIfAbsent值
//        List<Student> l1 = f1.getNow(new ArrayList<>());
//        List<TokenInfo> l2 = f2.getNow(new ArrayList<>());

        try {
            List<Student> l1 = f1.get();
            List<TokenInfo> l2 = f2.get();
            List<TT> calcu = calcu(l1, l2);
            System.out.println(calcu);
        } catch (Exception e) {

        }


    }


    private void futureTest() {
        CompletableFuture<Student> completableFutureOne = new CompletableFuture<>();

        // future 例子
        Future<List<Student>> dms = cachePool.submit(this::getDms);
        Future<List<TokenInfo>> dmsFs = cachePool.submit(this::getDmsFs);
        try {
            List<Student> students = dms.get();
            List<TokenInfo> tokenInfos = dmsFs.get();
            List<TT> calcu = calcu(students, tokenInfos);
            System.out.println(calcu);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    private List<Student> getDms() {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("查询dms的db，返回list");
        Student s = new Student(1, "1", 1);
        List<Student> l = new ArrayList();
        l.add(s);
        return l;
    }

    private List<TokenInfo> getDmsFs() {
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("查询dmsfs 的db，返回list");
        TokenInfo s = new TokenInfo();
        s.setAccess_token("2");
        s.setExpires_in(100L);
        List<TokenInfo> l = new ArrayList();
        l.add(s);
        return l;
    }


    public List<TT> calcu(List<Student> l, List<TokenInfo> tokens) {

        System.out.println("计算逻辑");
        TT tt = new TT();
        tt.setStudentList(l);
        tt.setTokenInfoList(tokens);
        List<TT> lt = new ArrayList();
        lt.add(tt);
        return lt;
    }


}
